home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / D-G / Extension Shell 1.3.sit / Extension Shell 1.3 ƒ / Sample Extensions / Shutdown Fade ƒ / Gamma.c / Gamma.c
Encoding:
Text File  |  1994-01-27  |  5.8 KB  |  192 lines  |  [TEXT/R*ch]

  1. /*    NAME:
  2.         Gamma.c
  3.  
  4.     WRITTEN BY:
  5.         Matt Slot
  6.  
  7.     MODIFIED BY:
  8.         Dair Grant
  9.                                 
  10.     DESCRIPTION:
  11.         Cut down version of Matt Slot's Gamma Utils Library source code. The
  12.         routines to deal with a given graphics device have been removed, and
  13.         the only calls now supported are those that work on all graphics
  14.         devices at once.
  15.  
  16.     ___________________________________________________________________________
  17. */
  18. // File "gamma.c" - Source for Altering the Gamma Tables of GDevices
  19. //   Last updated 3/13/93, MJS
  20. //
  21. // * ****************************************************************************** *
  22. //    Stripped down version for Shutdown Fade
  23. // * ****************************************************************************** *
  24. //
  25. //
  26. // * ****************************************************************************** *
  27. //
  28. //    This is the Source Code for the Gamma Utils Library file. Use this to build
  29. //        new functionality into the library or make an A4-based library. 
  30. //    See the header file "gamma.h" for much more information. -- MJS
  31. //
  32. // * ****************************************************************************** *
  33.  
  34. #include <GestaltEqu.h>
  35. #include <Quickdraw.h>
  36. #include <Video.h>
  37. #include <Traps.h>
  38. #include "gamma.h"
  39.  
  40.  
  41. globalGammasHdl    gammaTables=0;
  42.  
  43.  
  44. // * ****************************************************************************** *
  45. // * ****************************************************************************** *
  46.  
  47. Boolean IsGammaAvailable() {
  48.     GDHandle theGDevice;
  49.  
  50.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  51.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  52.     
  53.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice))
  54.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  55.                 TestDeviceAttribute(theGDevice, noDriver)) return(0);
  56.  
  57.     return(-1);
  58.     }
  59.  
  60.  
  61. // * ****************************************************************************** *
  62. // * ****************************************************************************** *
  63.  
  64. OSErr SetupGammaTools() {
  65.     short errorCold=0;
  66.     globalGammasHdl tempHdl;
  67.     GammaTblPtr    masterGTable;
  68.     GDHandle theGDevice;
  69.  
  70.     
  71.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  72.         if (errorCold = GetDevGammaTable(theGDevice, &masterGTable)) return(errorCold);
  73.         
  74.         tempHdl = (globalGammasHdl) NewHandle(sizeof(globalGammas));
  75.         if (tempHdl == 0) return(errorCold = MemError());
  76.         
  77.         (*tempHdl)->size = sizeof(GammaTbl) + masterGTable->gFormulaSize +
  78.                 (masterGTable->gChanCnt * masterGTable->gDataCnt * masterGTable->gDataWidth / 8);
  79.         (*tempHdl)->dataOffset = masterGTable->gFormulaSize;
  80.         (*tempHdl)->theGDevice = theGDevice;
  81.         
  82.         (*tempHdl)->saved = (GammaTblHandle) NewHandle((*tempHdl)->size);
  83.         if ((*tempHdl)->saved == 0) return(errorCold = MemError());
  84.         (*tempHdl)->hacked = (GammaTblHandle) NewHandle((*tempHdl)->size);
  85.         if ((*tempHdl)->hacked == 0) return(errorCold = MemError());
  86.     
  87.         BlockMove((Ptr) masterGTable, (Ptr) *(*tempHdl)->saved, (*tempHdl)->size);
  88.         
  89.         (*tempHdl)->next = gammaTables;
  90.         gammaTables = tempHdl;
  91.         }
  92.  
  93.     return(0);
  94.     }
  95.  
  96. // * ****************************************************************************** *
  97. // * ****************************************************************************** *
  98.  
  99. OSErr DoGammaFade(short percent) {
  100.     short errorCold=0;
  101.     register long size, i, theNum;
  102.     globalGammasHdl tempHdl;
  103.     unsigned char *dataPtr;
  104.  
  105.  
  106.  
  107.     for(tempHdl = gammaTables; tempHdl; tempHdl = (*tempHdl)->next) {
  108.     
  109.         BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  110.         dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  111.         size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  112.         
  113.         for(i=0; i < size; i++) {
  114.             theNum = dataPtr[i];
  115.             theNum = (theNum * percent) / 100;
  116.             dataPtr[i] = theNum;
  117.             }
  118.         
  119.         if (errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked))
  120.             return(errorCold);
  121.         }
  122.         
  123.     return(0);
  124.     }
  125.  
  126.  
  127. // * ****************************************************************************** *
  128. // * ****************************************************************************** *
  129.  
  130. OSErr DisposeGammaTools() {
  131.     globalGammasHdl tempHdl, nextHdl;
  132.  
  133.     for(tempHdl = gammaTables; tempHdl; tempHdl = nextHdl) {
  134.         nextHdl = (*tempHdl)->next;
  135.         DisposeHandle((Handle) (*tempHdl)->saved);
  136.         DisposeHandle((Handle) (*tempHdl)->hacked);
  137.         DisposeHandle((Handle) tempHdl);
  138.         }
  139.         
  140.     return(0);
  141.     }
  142.  
  143. // * ****************************************************************************** *
  144. // * ****************************************************************************** *
  145.  
  146. OSErr GetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  147.     short errorCold=0;
  148.     CntrlParam  *myCPB;
  149.  
  150.     ((long *) theTable)[0] = 0;
  151.  
  152.             
  153.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  154.     myCPB->csCode = cscGetGamma;
  155.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  156.     *(GammaTblPtr **) myCPB->csParam = theTable;
  157.     errorCold = PBStatus((ParmBlkPtr) myCPB, 0);
  158.  
  159.     DisposePtr((Ptr) myCPB);
  160.     return(errorCold);
  161.     }
  162.  
  163. // * ****************************************************************************** *
  164. // * ****************************************************************************** *
  165.  
  166. OSErr SetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  167.     CntrlParam *myCPB;
  168.     short errorCold=0;
  169.     CTabHandle cTab;
  170.     GDHandle saveGDevice;
  171.  
  172.  
  173.  
  174.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  175.     myCPB->csCode = cscSetGamma;
  176.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  177.     **(GammaTblPtr **) myCPB->csParam = *theTable;
  178.  
  179.     errorCold = PBControl((ParmBlkPtr) myCPB, 0);
  180.  
  181.     if (errorCold == 0) {
  182.         saveGDevice = GetGDevice();
  183.         SetGDevice(theGDevice);
  184.          cTab = (*(*theGDevice)->gdPMap)->pmTable;
  185.         SetEntries (0, (*cTab)->ctSize, (*cTab)->ctTable);
  186.         SetGDevice(saveGDevice);
  187.         }
  188.  
  189.     DisposePtr((Ptr) myCPB);
  190.     return (errorCold);
  191.     }
  192.